//
//  MineViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/2.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MineCell.h"
#import "MineTaskCell.h"





@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)MineHeaderView *headerView;
@property(nonatomic, strong)NSArray *item1Array;
@property(nonatomic, strong)NSArray *item2Array;
@property(nonatomic, strong)NSArray *sectionArray;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.item1Array = @[@{@"image":@"home",@"title":@"收藏"},
                        @{@"image":@"home",@"title":@"评论"},
                        @{@"image":@"home",@"title":@"历史"}];
    
    self.item2Array = @[@{@"image":@"home",@"title":@"钱包"},
                        @{@"image":@"home",@"title":@"设置"}];
    
    self.sectionArray = @[@(MineSectionType_login),@(MineSectionType_all),@(MineSectionType_task)];

    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (![UserAccountTool account].isLogin && section == MineSectionType_login) {
        return 0;
    }
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == MineSectionType_login || indexPath.section == MineSectionType_all) {
        return 50;
    }else{
        return 71;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = [UIColor xm_grayColor];
    return headerView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == MineSectionType_login) {
        return [UserAccountTool account].isLogin?self.item1Array.count:0;
    }else if (section == MineSectionType_all){
        return self.item2Array.count;
    }else if (section == MineSectionType_task){
        return 10;
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == MineSectionType_login) {
        MineCell * cell = [MineCell cellWithTableView:tableView];
        cell.dict = self.item1Array[indexPath.row];
        return cell;
    }else if (indexPath.section == MineSectionType_all){
        MineCell * cell = [MineCell cellWithTableView:tableView];
        cell.dict = self.item2Array[indexPath.row];
        return cell;
    }else if (indexPath.section == MineSectionType_task){
        MineTaskCell * cell = [MineTaskCell cellWithTableView:tableView];
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = self.headerView;
        _tableView = tableView;
    }
    return _tableView;
}
-(MineHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[MineHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    }
    return _headerView;
}




@end
